package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.hpf.Unit;
import com.sdry.service.hpf.UnitService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: UnitController 
 * @Description:包装单位Controller
 * @Author hpf
 * @Date 2018年9月21日 下午3:54:00 
 * @version 1.0
 */
@RequestMapping(value="/unitMana")
@Controller
public class UnitController {

	@Resource
	private UnitService unitService;
	
	/**
	 * 分页查询
	 * @param page 分页条件
	 * @param Unit 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageUnit")
	public void listPageUnit(Page page,Unit unit,HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (unit.getUtype() != null) {
			map.put("utype", unit.getUtype());
		} else {
			map.put("utype", "");
			unit.setUtype("");
		}
		int count = unitService.countUnit(unit);
		List<Unit> unitList = unitService.listPageUnit(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(unitList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存包装单位
	 * @param unit
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveUnit")
	public void saveUnit(Unit unit,HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (unit.getUid() == null || "".equals(unit.getUid())) {
			isSuccess = unitService.insertUnit(unit);
		} else {
			isSuccess = unitService.updateUnit(unit);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除包装单位
	 * @param uid
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteUnit")
	public void deleteUnit(Long uid,HttpServletResponse response) throws Exception{
		int isSuccess = unitService.deleteUnit(uid);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 批量删除包装单位
	 * @param idArr 包装单位ID数组
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteUnitByArr")
	public void deleteUnitByArr(@RequestParam("idArr[]") String[] idArr,HttpServletResponse response) throws Exception{
		int isSuccess = unitService.deleteUnits(idArr);
		ResponseUtil.write(response, isSuccess);
	}
}
